package com.example.ymm.modules1.controller.sys;

import com.example.ymm.dto.Constants;
import com.example.ymm.exception.YmException;
import com.example.ymm.model.CommonResult;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.controller.BaseController;
import com.example.ymm.modules1.entity.my.user.DbIntegralLog;
import com.example.ymm.modules1.entity.my.user.DbUser;
import com.example.ymm.modules1.entity.my.user.DbUserNote;
import com.example.ymm.modules1.service.dto.*;
import com.example.ymm.modules1.service.user.DbCardService;
import com.example.ymm.modules1.service.user.DbIntegralLogService;
import com.example.ymm.modules1.service.user.DbUserNoteService;
import com.example.ymm.modules1.service.user.DbUserService;
import com.example.ymm.util.UtilValidate;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/hl/sys/userManage")
@Tag(name= "pc手机登录用户管理")
@Slf4j
@CrossOrigin
public class SysUserManageController extends BaseController {

    @Autowired
    private DbUserService dbUserService;

    @Autowired
    DbUserNoteService dbUserNoteService;

    @Autowired
    DbIntegralLogService dbIntegralLogService;

    @Autowired
    DbCardService dbCardService;

    @Operation(summary= "列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public CommonResult list(@Valid DbUserQueryCriteria criteria, YmPageDto ymPage) {
        try{
            TableList<DbUser> list = dbUserService.list(criteria, ymPage);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Operation(summary= "读者时长统计列表")
    @RequestMapping(value = "/countList", method = RequestMethod.POST)
    public CommonResult countList(@Valid DbUserQueryCriteria criteria, YmPageDto ymPage) {
        try{
            TableList<DbUser> list = dbUserService.countList(criteria, ymPage);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }



    @Operation(summary= "查找特定userId")
    @RequestMapping(value = "/userLists", method = RequestMethod.POST)
    public CommonResult userLists(@Valid DbUserQueryCriteria criteria) {
        try{
            if(UtilValidate.isEmpty(criteria.getUserIds())){
                throw new YmException("参数缺失！");
            }
            List<String> list1 = Arrays.asList(criteria.getUserIds().split(","));
            criteria.setUserId(list1);
            List<DbUser> list = dbUserService.queryAll(criteria);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Operation(summary= "保存")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult save(DbUser dbUser) {
        try{
            dbUserService.editUser(dbUser);

            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Operation(summary= "详情")
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public CommonResult detail(@Parameter(name = "数据主键") @RequestParam(required = true) String userId) {
        try{
            DbUser dbUser1 = dbUserService.getById(userId);
            return CommonResult.success(dbUser1);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Operation(summary= "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public CommonResult delete(@Parameter(name = "数据主键") @RequestParam(required = true) String userId) {
        try{
            dbUserService.removeById(userId);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Operation(summary= "导入")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public CommonResult importExcel(@RequestBody ImportUserExcelDto dto) {
        try{
            dbUserService.importExcel(dto);
            System.out.println(dto);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Operation(summary= "读书记录列表")
    @RequestMapping(value = "/userNoteList", method = RequestMethod.POST)
    public CommonResult userNoteList(@Valid DbUserNoteQueryCriteria criteria, YmPageDto ymPage) {
        try{
            TableList<DbUserNote> list = dbUserNoteService.list(criteria, ymPage);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Operation(summary= "读书记录详情")
    @RequestMapping(value = "/userNoteDetail", method = RequestMethod.POST)
    public CommonResult userNoteDetail(String noteId) {
        try{
            DbUserNote note = dbUserNoteService.getById(noteId);
            return CommonResult.success(note,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }



    @Operation(summary= "参与统计")
    @RequestMapping(value = "/up", method = RequestMethod.POST)
    public CommonResult up(@Parameter(name = "数据主键") @RequestParam(required = true) String userId) {
        try{
            DbUser user = dbUserService.getById(userId);
            user.setIsRead(Constants.STATE_UP);
            dbUserService.saveOrUpdate(user);
            return CommonResult.success(user);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Operation(summary= "不参与统计")
    @RequestMapping(value = "/down", method = RequestMethod.POST)
    public CommonResult down(@Parameter(name = "数据主键") @RequestParam(required = true) String userId) {
        try{
            DbUser user = dbUserService.getById(userId);
            user.setIsRead(Constants.STATE_DOWN);
            dbUserService.saveOrUpdate(user);
            return CommonResult.success(user);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Operation(summary= "扣除用户积分")
    @RequestMapping(value = "/sysUserDeduct", method = RequestMethod.POST)
    public CommonResult sysUserDeduct(DbIntegralLog dbIntegralLog) {
        try{
            dbIntegralLogService.sysUserDeduct(dbIntegralLog);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Operation(summary= "用户增加读书积分")
    @RequestMapping(value = "/sysUserAddReadIntegral", method = RequestMethod.POST)
    public CommonResult sysUserAddReadIntegral(DbIntegralLog dbIntegralLog) {
        try{
            dbIntegralLogService.sysUserAddReadIntegral(dbIntegralLog);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Operation(summary= "用户 年分钟数列表")
    @RequestMapping(value = "/userYearCountList", method = RequestMethod.POST)
    public CommonResult userYearCountList(@Valid DbCardQueryCriteria criteria, YmPageDto ymPage) {
        try{

            TableList<UserTimeCountDto> list = dbCardService.getUserYearCount(criteria, ymPage);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Operation(summary= "用户 月分钟数列表")
    @RequestMapping(value = "/userMonthCountList", method = RequestMethod.POST)
    public CommonResult userMonthCountList(@Valid DbCardQueryCriteria criteria, YmPageDto ymPage) {
        try{

            TableList<UserTimeCountDto> list = dbCardService.getUserMonthCount(criteria, ymPage);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }



}
